package com.xjscrm.server.service.filterrule.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xjscrm.server.req.filterrule.CustomerFilterRuleReq;
import com.xjscrm.server.req.filterrule.CustomerTagsExcludeFilterReq;

import java.util.List;

/**
 * 用户标签排除过滤器
 */
public class CustomerTagsExcludeFilter extends AbstractCustomerExcludeFilter{

    private CustomerTagsExcludeFilter() {
    }

    private final static CustomerTagsExcludeFilter INSTANCE = new CustomerTagsExcludeFilter();


    /**
     * 根据条件和用户标签进行过滤，排除返回true,否则返回false
     * @param req
     * @return
     */
    @Override
    public Boolean filter(CustomerFilterRuleReq req) {
        CustomerTagsExcludeFilterReq customerTagsExcludeFilterReq = (CustomerTagsExcludeFilterReq) req;
        if (!customerTagsExcludeFilterReq.isOpenExclude()) {
            return false;
        }
        List<Long> wwCustomerTagedList = customerTagsExcludeFilterReq.getWwCustomerTagList();
        //客户企业标签选项(0-含任意标签；1-含所有标签；2-未打任何标签)
        Integer excludeCustomerEnterpriseTagOpt = customerTagsExcludeFilterReq.getExcludeCustomerEnterpriseTagOpt();
        if (excludeCustomerEnterpriseTagOpt == null) {
            return false;
        }
        //2-未打任何标签
        if (excludeCustomerEnterpriseTagOpt.intValue() == 2) {
            return CollectionUtils.isEmpty(wwCustomerTagedList);
        }
        //标签列表
        List<Long> customerEnterpriseTagList = JSONObject.parseArray(customerTagsExcludeFilterReq.getCustomerEnterpriseTags(), Long.class);
        if (CollectionUtils.isEmpty(customerEnterpriseTagList)) {
            return false;
        }
        if (excludeCustomerEnterpriseTagOpt.intValue() == 0) { //满足任意一个
            return customerEnterpriseTagList.stream()
                    .anyMatch(x -> wwCustomerTagedList.contains(x));
        }
        if (excludeCustomerEnterpriseTagOpt.intValue() == 1) {
            //满足所有触发
            return wwCustomerTagedList.containsAll(customerEnterpriseTagList);
        }
        return false;

    }

    public static CustomerTagsExcludeFilter getInstance() {
        return INSTANCE;
    }
}
